package com.scm.manager.impl;


import com.scm.common.core.domain.entity.SysUser;
import com.scm.converter.SupplyConverter;
import com.scm.dto.req.supply.ScmSupplyQuotationReq;
import com.scm.dto.resp.supply.ScmSupplyQuotationDetailResp;
import com.scm.manager.ScmSupplyQuotationManager;
import com.scm.persist.model.ScmSupplyQuotation;
import com.scm.persist.model.ScmSupplyQuotationDetail;
import com.scm.persist.model.ScmSupplyQuotationDetailExample;
import com.scm.persist.mybatis.ScmSupplyQuotationDetailMapper;
import com.scm.service.IScmSupplyQuotationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @Comment：
 * @Author: 邹渺
 * @Date: 2024/5/18
 * @Time: 上午10:29
 */
@Service
@Slf4j
public class ScmSupplyQuotationManagerImpl  implements ScmSupplyQuotationManager {


    @Autowired
    IScmSupplyQuotationService scmSupplyQuotationService;

    @Autowired
    ScmSupplyQuotationDetailMapper scmSupplyQuotationDetailMapper;


    @Override
    public List detailListByScmSupplyQuotation(Long id) {

        if(id == null){
            return Collections.emptyList();
        }

        ScmSupplyQuotationDetailExample example = new ScmSupplyQuotationDetailExample();
        ScmSupplyQuotationDetailExample.Criteria criteria = example.createCriteria();
        criteria.andSupplyQuotationIdEqualTo(id);
        List<ScmSupplyQuotationDetail> scmSupplyQuotationDetails = scmSupplyQuotationDetailMapper.selectByExample(example);
        List<ScmSupplyQuotationDetailResp> scmSupplyQuotationDetailResp = SupplyConverter.toScmSupplyQuotationDetailResp(scmSupplyQuotationDetails);
        return scmSupplyQuotationDetailResp;
    }


    @Override
    public int addSupplyQuotation(SysUser sysUser ,ScmSupplyQuotationReq scmSupplyQuotationReq) {
        ScmSupplyQuotation scmSupplyQuotation = SupplyConverter.toScmSupplyQuotation(scmSupplyQuotationReq);
        scmSupplyQuotation.setId(null);
        scmSupplyQuotation.setTenantId(sysUser.getTenantId());
        scmSupplyQuotationService.insertScmSupplyQuotation(scmSupplyQuotation);
        installDetail(scmSupplyQuotationReq, scmSupplyQuotation);
        return 1;
    }

    private void installDetail(ScmSupplyQuotationReq scmSupplyQuotationReq, ScmSupplyQuotation scmSupplyQuotation) {
        List<ScmSupplyQuotationDetail> scmSupplyQuotationDetail = SupplyConverter.toScmSupplyQuotationDetail(scmSupplyQuotationReq.getList());
        if(CollectionUtils.isNotEmpty(scmSupplyQuotationDetail)){
            for (ScmSupplyQuotationDetail supplyQuotationDetail : scmSupplyQuotationDetail) {
                supplyQuotationDetail.setSupplyQuotationId(scmSupplyQuotation.getId());
                supplyQuotationDetail.setCreateTime(System.currentTimeMillis());
                supplyQuotationDetail.setTenantId(scmSupplyQuotation.getTenantId());
                scmSupplyQuotationDetailMapper.insert(supplyQuotationDetail);
            }
        }
    }

    @Override
    public int updataSupplyQuotation(ScmSupplyQuotationReq scmSupplyQuotationReq) {
        ScmSupplyQuotation scmSupplyQuotation = SupplyConverter.toScmSupplyQuotation(scmSupplyQuotationReq);
        if(scmSupplyQuotation.getId() == null){
            return 0;
        }
        scmSupplyQuotationService.updateScmSupplyQuotation(scmSupplyQuotation);
        ScmSupplyQuotationDetailExample example = new ScmSupplyQuotationDetailExample();
        example.createCriteria().andSupplyQuotationIdEqualTo(scmSupplyQuotation.getId());
        scmSupplyQuotationDetailMapper.deleteByExample(example);
        installDetail(scmSupplyQuotationReq, scmSupplyQuotation);
        return 1;
    }
}
